class CustomersController < ApplicationController
  def index
    @customers = Customer.paginate(:page => params[:page], :per_page => 20, :order => "name")
  end

  def new
    @customer = Customer.new
    @customer.build_default_address
    @customer.build_default_contact
  end

  def create
    @customer = Customer.new(params[:customer])
    @customer.build_default_address(params[:address])
    @customer.build_default_contact(params[:contact])
    respond_to do |format|
      if @customer.save
        format.html  { redirect_to(customers_path,
                      :notice => 'Driver was successfully created.') }
        format.json  { render :json => @customer,
                      :status => :created, :location => @customer }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @customer.errors,
                      :status => :unprocessable_entity }
      end
    end
  end

  def edit
    @customer = Customer.find(params[:id])
    @customer.build_default_address if @customer.default_address.nil?
    @customer.build_default_contact if @customer.default_contact.nil?
  end

  def update
    @customer = Customer.find(params[:id])
    @customer.build_default_address(params[:address])
    @customer.build_default_contact(params[:contact])
    respond_to do |format|
      if @customer.update_attributes(params[:customer])
        format.html  { redirect_to(customers_path,
                      :notice => 'Driver was successfully created.') }
        format.json  { render :json => @customer,
                      :status => :created, :location => @customer }
      else
        format.html  { render :action => "new" }
        format.json  { render :json => @customer.errors,
                      :status => :unprocessable_entity }
      end
    end
  end
end
